// pages/index/index.js  
Page({  
  data: {  
    board: Array.from({ length: 15 }, () => Array(15).fill(null)),  
    currentPlayer: 'black',  
    message: ''  
  },  
  
  getCellColor(row, col) {  
    const cell = this.data.board[row][col];  
    return cell === 'black' ? 'black' : cell === 'white' ? 'white' : '';  
  },  
  
  handleTap(e) {  
    const { row, col } = e.currentTarget.dataset;  
    const { board, currentPlayer } = this.data;  
  
    if (board[row][col] !== null) return;  
  
    board[row][col] = currentPlayer;  
    this.setData({ board });  
  
    const winner = this.checkWinner(row, col);  
    if (winner) {  
      this.setData({  
        message: `${winner} wins!`,  
        currentPlayer: '' // No more moves allowed  
      });  
    } else {  
      this.setData({  
        currentPlayer: currentPlayer === 'black' ? 'white' : 'black'  
      });  
    }  
  },  
  
  checkWinner(row, col) {  
    const directions = [  
      { dr: 1, dc: 0 }, { dr: -1, dc: 0 }, // Horizontal  
      { dr: 0, dc: 1 }, { dr: 0, dc: -1 }, // Vertical  
      { dr: 1, dc: 1 }, { dr: -1, dc: -1 }, // Diagonal \  
      { dr: 1, dc: -1 }, { dr: -1, dc: 1 }  // Diagonal /  
    ];  
    const { board, currentPlayer } = this.data;  
    const count = (dr, dc) => {  
      let count = 1;  
      for (let i = 1; i < 5; i++) {  
        const r = row + i * dr;  
        const c = col + i * dc;  
        if (r >= 0 && r < 15 && c >= 0 && c < 15 && board[r][c] === currentPlayer) {  
          count++;  
        } else {  
          break;  
        }  
      }  
      for (let i = 1; i < 5; i++) {  
        const r = row - i * dr;  
        const c = col - i * dc;  
        if (r >= 0 && r < 15 && c >= 0 && c < 15 && board[r][c] === currentPlayer) {  
          count++;  
        } else {  
          break;  
        }  
      }  
      return count;  
    };  
  
    for (const { dr, dc } of directions) {  
      if (count(dr, dc) >= 5) {  
        return currentPlayer;  
      }  
    }  
    return null;  
  }  
});